package com.yanze.service.impl;

import com.yanze.mapper.UserMapper;
import com.yanze.mapper.UserMapperCustom;
import com.yanze.pojo.User;
import com.yanze.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceimpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserMapperCustom userMapperCustom;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void insertUser(User user) {
        userMapper.insert(user);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public User queryUserByPhone(String userphone) {
        List<User> users = userMapperCustom.queryUserByPhone(userphone);
        if (users != null && !users.isEmpty()) {
            return users.get(0);
        }
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public User queryUserById(String userid) {
        List<User> users = userMapperCustom.queryUserById(userid);
        if (users != null && !users.isEmpty()) {
            return users.get(0);
        }
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateUser(User user) {
        int flag = userMapper.updateByPrimaryKeySelective(user);
        return flag;
    }
}
